﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { PasswordComplexitySetting, ProfileServiceProxy, UserEditDto, UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { TestDataDto } from '@shared/service-proxies/service-proxies';
import { StandardPartsDetailServiceProxy } from '@shared/service-proxies/service-proxies';
@Component({
    selector: 'workAssignmentEditComponent',
    templateUrl: './workassignment_edit.component.html',
    styles: [`.user-edit-dialog-profile-image {
             margin-bottom: 20px;
        }`
    ]
})
export class WorkAssignmentEditComponent extends AppComponentBase {

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    //canChangeUserName = true;
    //isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled');
    //isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled');
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();
    formData: TestDataDto = new TestDataDto();
    //standardPartsFeature: StandardPartsFeatureDto=new StandardPartsFeatureDto()
    user: UserEditDto = new UserEditDto();
    //roles: UserRoleDto[];
    //sendActivationEmail = true;
    //setRandomPassword = true;
    passwordComplexityInfo = '';
    profilePicture: string;
    fieldDate: Date;

    //allOrganizationUnits: OrganizationUnitDto[];
    //memberedOrganizationUnits: string[];
    
    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _standardPartsDetailServiceProxy: StandardPartsDetailServiceProxy,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
    }

    show(standardPartsDetail?: TestDataDto): void {
        if (standardPartsDetail) {
            this.formData = standardPartsDetail;
            this.fieldDate = standardPartsDetail.requiredTime.toDate();
            //this.standardPartsFeature = standardPartsDetail.features;
        } else {
            this.formData = new TestDataDto();
            //this.standardPartsFeature = new StandardPartsFeatureDto();
        }
        this.active = true;
        this.modal.show();
    }

    setPasswordComplexityInfo(): void {

        this.passwordComplexityInfo = '<ul>';

        if (this.passwordComplexitySetting.requireDigit) {
            this.passwordComplexityInfo += '<li>' + this.l('PasswordComplexity_RequireDigit_Hint') + '</li>';
        }

        if (this.passwordComplexitySetting.requireLowercase) {
            this.passwordComplexityInfo += '<li>' + this.l('PasswordComplexity_RequireLowercase_Hint') + '</li>';
        }

        if (this.passwordComplexitySetting.requireUppercase) {
            this.passwordComplexityInfo += '<li>' + this.l('PasswordComplexity_RequireUppercase_Hint') + '</li>';
        }

        if (this.passwordComplexitySetting.requireNonAlphanumeric) {
            this.passwordComplexityInfo += '<li>' + this.l('PasswordComplexity_RequireNonAlphanumeric_Hint') + '</li>';
        }

        if (this.passwordComplexitySetting.requiredLength) {
            this.passwordComplexityInfo += '<li>' + this.l('PasswordComplexity_RequiredLength_Hint', this.passwordComplexitySetting.requiredLength) + '</li>';
        }

        this.passwordComplexityInfo += '</ul>';
    }

    getProfilePicture(profilePictureId: string): void {
        if (!profilePictureId) {
            this.profilePicture = this.appRootUrl() + 'assets/common/images/default-profile-picture.png';
        } else {
            this._profileService.getProfilePictureById(profilePictureId).subscribe(result => {

                if (result && result.profilePicture) {
                    this.profilePicture = 'data:image/jpeg;base64,' + result.profilePicture;
                } else {
                    this.profilePicture = this.appRootUrl() + 'assets/common/images/default-profile-picture.png';
                }
            });
        }
    }

    onShown(): void {
        document.getElementById('code').focus();
    }
    save(): void {
        /*
        this.standardPartsDetail.features=this.standardPartsFeature;
        this.saving = true;
        if(!this.standardPartsDetail.id){
            this._standardPartsDetailServiceProxy.createDetail(this.standardPartsDetail)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe(() => {
                    this.notify.info(this.l('SavedSuccessfully'));
                    this.close();
                    this.modalSave.emit(null);
                });
        }else{
            this._standardPartsDetailServiceProxy.updateDetail(this.standardPartsDetail)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe(() => {
                    this.notify.info(this.l('SavedSuccessfully'));
                    this.close();
                    this.modalSave.emit(null);
                });
        }
        this.standardPartsDetail = new StandardPartsDetailDto();
        */
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
/*
    getAssignedRoleCount(): number {
        return _.filter(this.roles, { isAssigned: true }).length;
    }*/
}
